import { Http } from "../../utils/http"
import { Order } from "../../model/Order"

Page({

  data: {
    goods_list: [],
    // tabs的初始数据
    tabs: [{
      id: 0,
      name: "全部",
      isActive: true
    },
    {
      id: 1,
      name: "待付款",
      isActive: false
    },
    {
      id: 2,
      name: "已付款",
      isActive: false
    },
    {
      id: 3,
      name: "已核销",
      isActive: false
    }],
    page:1,
    size:5
  },

  //生命周期函数--监听页面加载
  onLoad: async function (options) {
    let a = Http.login();
    let order = await Order.get_order_by_user(1,100); //用户的订单列表 传入:page,size

    this.loadData(order);
  },

  // tab点击的事件
  handleItemChange(e) {
    const { index } = e.detail;
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    });
  },
  
  //监听用户下拉动作
  onPullDownRefresh: async function () {
    this.onLoad();
  },
  
  //上拉加载 下拉刷新
  loadData: async function (order) {
    let answer = this.data.goods_list;
    if (order.code == 0) {
      wx.stopPullDownRefresh();
      for (let index = 0; index < order.data.data.length; index++) {
        answer.push(order.data.data[index]);
      }
      this.setData({
        goods_list: order.data.data
      })
    } else {
      console.log("获取失败...");
    }
  },

  qrCode: function (e) {
    // console.log(e.currentTarget.dataset['id']);
    // console.log(e.currentTarget.dataset['status']);
    // 判断能否跳转进二维码界面  2 已付款  3待核销
    wx.navigateTo({
      url: '../qrcode/index?status=' + e.currentTarget.dataset['status']+"&id="+ e.currentTarget.dataset['id']+"&order_no="+e.currentTarget.dataset['order_no'],
    })
    if (e.currentTarget.dataset['status'] == 1 || e.currentTarget.dataset['status'] == 2) {
    }
  },

  //监听用户上拉触底事件
  onReachBottom: function () {
      console.log('触底');
      this.setData({
        page: this.data.page+1,
        size: this.data.size
      })
      this.onLoad();
  }
})